import 'package:flutter/material.dart';
import '/model/chat_list_model.dart';
import '/utils/setting.dart';
import '/utils/load_image.dart';

// 微信聊天列表的每一条记录
class ChatListItem extends StatelessWidget {
  final ChatListItemModel model; // 聊天列表的模型属性

  const ChatListItem({
    super.key,
    required this.model
  });

  @override
  Widget build(BuildContext context) {

    double avatarSize = 50.0;

    // 头像组件
    Widget avatarWidget = ClipRRect(
      borderRadius: BorderRadius.circular(10.0),
      child: loadImageByUrl(model.avatarUrl, avatarSize, avatarSize),
    );

    // 昵称
    Widget nickNameWidget = Text(
        model.userName,
        style: const TextStyle(
            fontSize: 18.0,
            fontWeight: FontWeight.w400
        )
    );

    // 短消息
    Widget msgWidget = Text(
      model.message,
      style: const TextStyle(fontSize: 12.0, color: DefaultColor.word),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );

    // 消息时间
    Widget msgTimeWidget = Text(
        model.msgTime,
        textAlign: TextAlign.end,
        style: const TextStyle(fontSize: 12.0, color: DefaultColor.word)
    );

    return InkWell(
      onTap: () {
        debugPrint('点击了${model.userName}');
        Navigator.pushNamed(context, 'ChatWindowPage', arguments: {
          'userName': model.userName,
          'avatarUrl': model.avatarUrl
        });
      },
      child: Column(
        children: [
          Row(children: [
            avatarWidget, // 头像
            const SizedBox(width: 8.0),
            Expanded(
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                            flex: 70,
                            child: nickNameWidget, // 昵称
                        ),
                        // const SizedBox(width: 8.0),
                        Expanded(
                            flex: 30,
                            child: msgTimeWidget, // 消息时间
                        )
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          flex: 90,
                          child: msgWidget, // 短消息
                        ),

                        // const SizedBox(width: 8.0),
                        if (model.isShield != null && model.isShield == true)
                          const Expanded(
                            flex: 10,
                            child: Icon(Icons.notifications_off_outlined, size: 16, color: DefaultColor.word), // 屏蔽图标
                          )
                      ],
                    ),
                  ]),
            ),
          ]),
          const Divider(
            thickness: 1.0,
            indent: 4, // 设置分割线距离上一个子组件（上面内容）的距离
            endIndent: 4, // 设置分割线距离下一个子组件（下面内容）的距离
            color: DefaultColor.primary,
          )
        ],
      ),
    );
  }
}
